local skels = {}

local skel = fk.CreateSkill {
  name = "rmt__hemeng",
}

skel:addEffect(fk.CardUsing, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and
    ((data.card.multiple_targets and data.card.type == Card.TypeTrick)
    or (data.card.name == "fire__slash" or data.card.name == "thunder__slash" or data.card.name == "ice__slash"))
    -- 毒杀刺杀不算属性杀
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return not target:isProhibited(p, data.card)
      and data.card.skill:modTargetFilter(data.from, p, {}, data.card, {bypass_distances = true})
    end)
    if #targets == 0 then return false end
    local num = math.min(#room.alive_players // 2, #targets)
    local prompt = "#rmt__hemeng-"..(target ~= player and "mustself" or "choose")..":::"..data.card:toLogString()..":"..num
    local _, dat = room:askToUseActiveSkill(player, {
      skill_name = "rmt__hemeng_choose", cancelable = true, prompt = prompt,
      extra_data = {targetIds = table.map(targets, Util.IdMapper), target_num = num, must_self = target ~= player},
    })
    if dat then
      room:sortByAction(dat.targets)
      event:setCostData(self, {tos = dat.targets})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    data.tos = table.simpleClone(tos)
    room:sendLog{type = "#RmtChangeTarget", from = target.id, arg = data.card:toLogString(), to = table.map(tos, Util.IdMapper)  }
  end,
})

Fk:loadTranslationTable{
  ["rmt__hemeng"] = "合盟",
  [":rmt__hemeng"] = "一名角色使用属性【杀】或多目标锦囊牌时，你可以将此牌目标改为场上半数由你指定的角色（向下取整，不足则全选）；若此牌使用者不为你，你需为目标之一。",
  ["#rmt__hemeng-choose"] = "合盟：你可重新选择 %arg2 名角色作为%arg的目标",
  ["#rmt__hemeng-mustself"] = "合盟：你可重新选择 %arg2 名角色作为%arg的目标（须包含你！）",
  ["rmt__hemeng_choose"] = "合盟",
  ["#RmtChangeTarget"] = "%from 使用的 %arg 目标被改为 %to",

  ["$rmt__hemeng1"] = "北伐山戎、南至召陵；九合诸侯，一匡天下！",
  ["$rmt__hemeng2"] = "凡我同盟之人，既盟之后，言归于好。",
}

local skel2 = fk.CreateSkill {
  name = "rmt__hemeng_choose",
}

skel2:addEffect("active", {
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return table.contains(self.targetIds or {}, to_select.id) and #selected < self.target_num
  end,
  feasible = function (self, player, selected, selected_cards, card)
    if self.must_self and not table.contains(selected, player) then return false end
    return #selected == self.target_num
  end,
})

table.insert(skels, skel)
table.insert(skels, skel2)

return skels
